/*Copyright (c) 2012 Nicolás Zalaquett

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions of
the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
package extrae;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
import javafx.scene.shape.Polyline;
import javax.swing.JOptionPane;

/**
 * @author lojkoro
 */

 /*Osciloscopio: clase visual para la representación a tiempo real
  *de los datos que llegan de los canales seleccionados.
  *TODO: Zoom por zona y otras similares*/
public class Osciloscopio extends CustomNode{
    public var pAN:parametrosAnalogo=parametrosAnalogo{};
    public var numeroDatos=0;
    public-read def color2: javafx.scene.paint.Color = javafx.scene.paint.Color {
        red: 0.6
        green: 0.6
        blue: 0.6
    }
    public-read def color3: javafx.scene.paint.Color = javafx.scene.paint.Color {
    }
    public-read def color4: javafx.scene.paint.Color = javafx.scene.paint.Color {
        red: 1.0
        green: 0.2
        blue: 0.2
    }
    public-read def color5: javafx.scene.paint.Color = javafx.scene.paint.Color {
        green: 0.8
    }
    public-read def color6: javafx.scene.paint.Color = javafx.scene.paint.Color {
        red: 0.2
        green: 0.2
        blue: 1.0
    }
    public var datos:ArregloDatos[]=[ArregloDatos{},ArregloDatos{},ArregloDatos{},ArregloDatos{}];
    public var minX:Number=0;
    public var maxX:Number=500;
    public var minY:Number[]=[0,0,0];
    public var maxY:Number[]=[1024,1024,1024];
    public var labelMaxY:Label[]=[
        Label {
        layoutX: 15.0
        layoutY: -15.0
        text: bind "{pAN.canalSensor[0].rangoMax} {pAN.canalSensor[0].unidades}"
        textFill: color6
        visible:bind pAN.canalActivo[0]
             },  
        Label {
        layoutX: 15.0
        layoutY: 0.0
        text: bind "{pAN.canalSensor[1].rangoMax} {pAN.canalSensor[1].unidades}"
        textFill: color4
        visible:bind pAN.canalActivo[1]
            },
        Label {
        layoutX: 15.0
        layoutY: 15.0
        text: bind "{pAN.canalSensor[2].rangoMax} {pAN.canalSensor[2].unidades}"
        textFill: color5
        visible:bind pAN.canalActivo[2]
        }
        ];
    public var labelMinY:Label[]=[
        Label {
        layoutX: 15.0
        layoutY: 345.0
        text: bind "{pAN.canalSensor[0].rangoMin} {pAN.canalSensor[0].unidades}"
        textFill: color6
        visible:bind pAN.canalActivo[0]
    },
        Label {
        layoutX: 15.0
        layoutY: 360.0
        text: bind "{pAN.canalSensor[1].rangoMin} {pAN.canalSensor[1].unidades}"
        textFill: color4
        visible:bind pAN.canalActivo[1]
    },
        Label {
        layoutX: 15.0
        layoutY: 375.0
        text: bind "{pAN.canalSensor[2].rangoMin} {pAN.canalSensor[2].unidades}"
        textFill: color5
        visible:bind pAN.canalActivo[2]
    }];
    public var labelDivisionY:Label[]=[
        Label {
        layoutX: 20.0
        layoutY: 145.0
        text: bind "{pAN.canalSensor[0].resolucion}"
        textFill: color6
        visible:false
    },
        Label {
        layoutX: 20.0
        layoutY: 160.0
        text: bind "{pAN.canalSensor[1].resolucion}"
        textFill: color4
        visible:false
    },
        Label {
        layoutX: 20.0
        layoutY: 175.0
        text: bind "{pAN.canalSensor[2].resolucion}"
        textFill: color5
        visible:false
    }];
    public var polyLineCanal:Polyline[]=[
        Polyline {
        layoutX: 0.0
        layoutY: 300.0
        stroke: color6
        points: null
    },
        Polyline {
        layoutX: 0.0
        layoutY: 300.0
        stroke: color4
        points: null
    },
        Polyline {
        layoutX: 0.0
        layoutY: 300.0
        stroke: color5
        points: null
    }];
    // <editor-fold defaultstate="collapsed" desc="Diseño">
    public-read def botonEjesDefault: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 414.0
        layoutY: 371.0
        text: "Ejes Iniciales"
        visible:false
    }
    def __layoutInfo_botonMasX: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonMasX: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 15.0
        layoutY: 0.0
        layoutInfo: __layoutInfo_botonMasX
        text: "+"
    }
    def __layoutInfo_botonMenosX: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonMenosX: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 0.0
        layoutY: 0.0
        layoutInfo: __layoutInfo_botonMenosX
        text: "-"
    }
    def __layoutInfo_botonDerechaX: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonDerechaX: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 15.0
        layoutY: 15.0
        layoutInfo: __layoutInfo_botonDerechaX
        text: ">"
    }
    def __layoutInfo_botonIzquierdaX: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonIzquierdaX: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 0.0
        layoutY: 15.0
        layoutInfo: __layoutInfo_botonIzquierdaX
        text: "<"
    }
    public-read def panel2: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 350.0
        layoutY: 365.0
        content: [ botonMasX, botonMenosX, botonDerechaX, botonIzquierdaX, ]
        visible:false
    }
    def __layoutInfo_botonMasY: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonMasY: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 0.0
        layoutY: 0.0
        layoutInfo: __layoutInfo_botonMasY
        text: "+"
    }
    def __layoutInfo_botonMenosY: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonMenosY: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 0.0
        layoutY: 15.0
        layoutInfo: __layoutInfo_botonMenosY
        text: "-"
    }
    def __layoutInfo_botonArribaY: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonArribaY: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 15.0
        layoutY: 0.0
        layoutInfo: __layoutInfo_botonArribaY
        text: "A"
    }
    def __layoutInfo_botonAbajoY: javafx.scene.layout.LayoutInfo = javafx.scene.layout.LayoutInfo {
        width: 12.0
        height: 12.0
    }
    public-read def botonAbajoY: javafx.scene.control.Button = javafx.scene.control.Button {
        layoutX: 15.0
        layoutY: 15.0
        layoutInfo: __layoutInfo_botonAbajoY
        text: "v"
    }
    public-read def panel3: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 20.0
        layoutY: 200.0
        content: [ botonMasY, botonMenosY, botonArribaY, botonAbajoY, ]
        visible:false
    }
    public-read def labelMaxX: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 543.0
        layoutY: 350.0
        text: "Label"
        visible:false
    }
    public-read def labelMinX: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 68.0
        layoutY: 350.0
        text: "Label"
        visible:false
    }
    public-read def labelDivisionX: javafx.scene.control.Label = javafx.scene.control.Label {
        layoutX: 300.0
        layoutY: 372.0
        text: "Label"
        visible:false
    }
    public-read def color: javafx.scene.paint.Color = javafx.scene.paint.Color {
        red: 0.9098039
        green: 0.90588236
        blue: 0.90588236
    }
    public-read def rectangle: javafx.scene.shape.Rectangle = javafx.scene.shape.Rectangle {
        layoutX: -1.0
        layoutY: -1.0
        fill: color
        stroke: javafx.scene.paint.Color.BLACK
        width: 500.0
        height: 300.0
    }
    public-read def panel: javafx.scene.layout.Panel = javafx.scene.layout.Panel {
        layoutX: 70.0
        layoutY: 40.0
        content: [ rectangle, polyLineCanal ]
    }
    public var gridX:Line[];
    public var gridY:Line[];
    //</editor-fold>
    public override function create(): Node {

        for (i in [0..20]) {
            var linX:Line=Line{
                        startX: i*25
                        startY: 0
                        endX: i*25
                        endY: 298
                        stroke:Color.LIGHTGRAY
                };
            insert linX into gridX;
            }
        for (i in [0..20]) {
            var linY:Line=Line{
                        startX: 0
                        startY: i*15
                        endX: 498
                        endY: i*15
                        stroke:Color.LIGHTGRAY
                };
            insert linY into gridY;
            }
        insert gridX into panel.content;
        insert gridY into panel.content;
        return Group {
                    content: [
                        panel, botonEjesDefault, panel2, panel3, labelMinY, labelMaxX, labelMinX,labelDivisionY,labelMaxY, labelDivisionX
                    ]
                };
    }
    public function addDato(canal:Boolean[], dato:Number[], tiempo:Number):Void{

        var dato2:Number[]=[0,0,0];

        if (numeroDatos < maxX){
            for (i in [0..2]){
               if (canal[i]){
                    dato2[i]=-dato[i]*300/1023;
                    insert [numeroDatos,dato2[i]] into polyLineCanal[i].points;
                    insert tiempo*numeroDatos into datos[3].Datos;
                    insert (dato[i]*pAN.canalSensor[i].pendiente + pAN.canalSensor[i].corte) into datos[i].Datos;
                }
            }
            numeroDatos++;
        }
        else{
            numeroDatos=0;
            for (i in [0..2]){
               if (canal[i]){
                    dato2[i]=-dato[i]*300/1023;
                    polyLineCanal[i].points=[];
                    datos[3].Datos=[];
                    datos[i].Datos=[];
                    insert [numeroDatos,dato2[i]] into polyLineCanal[i].points;
                    insert tiempo*numeroDatos into datos[3].Datos;
                    insert (dato[i]*pAN.canalSensor[i].pendiente + pAN.canalSensor[i].corte) into datos[i].Datos;
                }
            }
            numeroDatos++;
            }
        }

}
